//
//  NBDiscoverViewController.m
//  聚超值
//
//  Created by 李智恒 on 16/3/27.
//  Copyright © 2016年 Zhli. All rights reserved.
//

#import "NBDiscoverViewController.h"
#import "UIImage+Image.h"
#import "NBDiscoverCell.h"
#import "NBDiscoverItem.h"
#import "ZHPullDownMenuController.h"

#import "ZHHttpTool.h"

#import "MJExtension.h"
#import "MJRefresh.h"

#import "NBTwoTypeItem.h"
#import "NBTypeItem.h"
#import "JSDropDownMenu.h"



#define ZHScreenW [UIScreen mainScreen].bounds.size.width
#define ZHScreenH [UIScreen mainScreen].bounds.size.height

static NSString * const ID = @"NBDiscoverCell";

@interface NBDiscoverViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate,JSDropDownMenuDataSource,JSDropDownMenuDelegate>

@property (nonatomic,strong) NSArray *item;

@property (nonatomic,strong) UICollectionView *CollectionView;

//存放页码的属性
@property (nonatomic,assign) NSInteger page;
//存放全部模型数据的数组
@property (nonatomic,strong) NSMutableArray *Array;


@property (nonatomic,strong) NSArray *data;
@property (nonatomic,strong) NSMutableArray *data1;
@property (nonatomic,strong) NSMutableArray *data2;
@property (nonatomic,strong) NSMutableArray *data3;


@property (nonatomic,strong) NSArray *twodata1;
@property (nonatomic,strong) NSArray *twodata2;
@property (nonatomic,strong) NSArray *twodata3;
@property (nonatomic,strong) NSString *mallId;
@property (nonatomic,strong) NSString *tagId;
@property (nonatomic,strong) NSString *typeId;


@property (nonatomic,assign) NSInteger currentData1Index;
@property (nonatomic,assign) NSInteger currentData2Index;
@property (nonatomic,assign) NSInteger currentData3Index;

@property (nonatomic,assign) NSInteger currentData1SelectedIndex;
@property (nonatomic,strong)  JSDropDownMenu *menu;






@end

@implementation NBDiscoverViewController
//隐藏状态栏
-(BOOL)prefersStatusBarHidden
{
    return YES;
}
-(NSMutableArray *)Array
{
    if (_Array==nil) {
        _Array=[NSMutableArray array];
    }
    return _Array;
}
//刷新数据方法
-(void)reloadData
{
    
    //上拉刷新(MJ刷新框架)
    self.CollectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        //页码+1
        self.page++;
        //请求数据
        
        NSString *url=[NSString stringWithFormat:@"http://mrobot.pconline.com.cn/s-300/best/faxian/getTopicList.xsp?mallId=0&tagId=0&typeId=0&pageNo=%ld&pageSize=20&v=2.3.0&platform=android",self.page];
        
        [self Get:url];
        
    }];
    
    //下拉刷新(MJ刷新框架)
    self.CollectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        //清空旧数据
        self.Array=nil;
        
        self.page=1;
        
        NSString *url=[NSString stringWithFormat:@"http://mrobot.pconline.com.cn/s-300/best/faxian/getTopicList.xsp?mallId=%@&tagId=%@&typeId=%@&pageNo=1&pageSize=20&v=2.3.0&platform=android",self.mallId,self.tagId,self.typeId];
        
        [self Get:url];
        
        
    }];


}
-(void)viewDidLoad
{
    
    //设置导航栏左边按钮
    [self AddLeftButton];
    
    //设置导航栏右边按钮
    [self AddRigthButton];
    
    
//    //请求数据
//    [self Get:@"http://mrobot.pconline.com.cn/s-300/best/faxian/getTopicList.xsp?mallId=0&tagId=0&typeId=0&pageNo=1&pageSize=20&v=2.3.0&platform=android"];
//    
//    //设置默认页码为1
//    self.page=1;
    
    //创建流水布局
    UICollectionViewFlowLayout *layout=({
        
        UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
        
        CGFloat X=(ZHScreenW-1)/2;
        
        layout.itemSize=CGSizeMake(X, 280);
        
        layout.minimumInteritemSpacing=1;
        
        layout.minimumLineSpacing=1;
        
        layout;
        
    });
    
    
    UICollectionView *collection=({
        
        UICollectionView *collection = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        
        collection.frame=CGRectMake(0, 45, ZHScreenW, ZHScreenH);
        
        collection.backgroundColor=[UIColor grayColor];
        
        [self.view addSubview:collection];
        
        //设置数据源
        collection.dataSource=self;
        
        self.CollectionView=collection;
        
        collection;
        
    });
    
    //注册Cell
    [collection registerNib:[UINib nibWithNibName:@"NBDiscoverCell" bundle:nil] forCellWithReuseIdentifier:ID];
    
    //设置代理
    self.CollectionView.delegate=self;
    
    
    //0.告诉框架新值和旧值的对应关系
    [NBTwoTypeItem mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
        return @{
                 @"ID":@"id",
                 };
    }];
    
    
    //初始化下拉筛选菜单
    [self SetUpData];
    
    _data1=[NSMutableArray array];
    
    _data2=[NSMutableArray array];
    
    _data3=[NSMutableArray array];
    
    //设置刷新数据
    [self reloadData];
    
    // 马上进入刷新状态
    [self.CollectionView.header beginRefreshing];

    
    [super viewDidLoad];
}
//请求数据方法
-(void)Get:(NSString *)url
{
    ////http://mrobot.pconline.com.cn/s-300/best/faxian/getTopicList.xsp?mallId=1&tagId=19&typeId=288&pageNo=1&pageSize=20&v=2.3.0&platform=android
    
//    NSString *url=[NSString stringWithFormat:@"http://mrobot.pconline.com.cn/s-300/best/faxian/getTopicList.xsp?mallId=0&tagId=0&typeId=0&pageNo=%ld&pageSize=20&v=2.3.0&platform=android",page];
    
    [ZHHttpTool get:url params:nil success:^(id responseObj) {
        
        self.item = [NBDiscoverItem mj_objectArrayWithKeyValuesArray:responseObj[@"data"]];
        
         [self.Array addObjectsFromArray:_item];
        
        [self.CollectionView reloadData];

        [self.CollectionView.mj_header endRefreshing];
        
        [self.CollectionView.mj_footer endRefreshing];

        
        //判断加载数据是否为空
        if (self.item.count==0) {
            //如果为空
            //停止加载数据
            [self.CollectionView.mj_footer endRefreshing];
            //提示数据已经全部加载完成
            [self.CollectionView.mj_footer endRefreshingWithNoMoreData];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
        //打印错误信息
        NSLog(@"%@",error);
        //数据请求失败,页数--
        self.page--;
    }];
   
    
}


//设置数据源方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    

    return self.Array.count;
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
     NBDiscoverCell  *cell=[collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    cell.items=self.Array[indexPath.row];
    
    return cell;
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld",indexPath.row);
}


-(void)leftBtnClick
{
    NSLog(@"点击了左边按钮");
    
}

-(void)rigthBtnClick
{
    NSLog(@"点击了右边按钮");
}



//添加导航左边按钮方法
-(void)AddLeftButton
{
    //设置导航栏左边按钮
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageOriginalWithName:@"pcobb_navbar_btn_search"] style:(UIBarButtonItemStyleDone) target:self action:@selector(leftBtnClick)];

}

//添加导航右边按钮方法
-(void)AddRigthButton
{
    //设置导航栏右边按钮
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageOriginalWithName:@"pcobb_navbar_btn_uc"] style:(UIBarButtonItemStyleDone) target:self action:@selector(rigthBtnClick)];
}


//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
//{
//    //判断当前用户是否正在下拉刷新
//    if (scrollView.contentOffset.y<0) {
//        
//        
//    }
//    //判断用户是否正在上拉刷新
//    else if (scrollView.contentOffset.y>1520)
//    {
//        
//    }
//    
//    
//}

//----------------------------------------------------------------------
//设置下拉筛选菜单初始化方法
-(void)SetUpData
{
    // 指定默认选中
    _currentData1Index = 0;
    _currentData1SelectedIndex = 1;
    
    _data1=[NSMutableArray arrayWithObjects:@"全部商城", nil];
    _data2=[NSMutableArray arrayWithObjects:@"全部热点", nil];
    _data3=[NSMutableArray arrayWithObjects:@"全部推荐", nil];
    
    [self GetType:@"ss"];
    
    _menu = [[JSDropDownMenu alloc] initWithOrigin:CGPointMake(0, 44) andHeight:45];
    _menu.indicatorColor = [UIColor colorWithRed:175.0f/255.0f green:175.0f/255.0f blue:175.0f/255.0f alpha:1.0];
    _menu.separatorColor = [UIColor colorWithRed:210.0f/255.0f green:210.0f/255.0f blue:210.0f/255.0f alpha:1.0];
    _menu.textColor = [UIColor colorWithRed:83.f/255.0f green:83.f/255.0f blue:83.f/255.0f alpha:1.0f];
    _menu.dataSource = self;
    _menu.delegate = self;
    
    
    [self.view addSubview:_menu];
    
}
//请求数据
-(void)GetType:(NSString *)type
{
    
    
    NSString *url=[NSString stringWithFormat:@"http://mrobot.pconline.com.cn/s/best/faxian/screen.xsp?v=2.3.0&platform=android"];
    
    [ZHHttpTool get:url params:nil success:^(id responseObj) {
        
        self.data = [NBTypeItem mj_objectArrayWithKeyValuesArray:responseObj[@"data"]];
        
        NBTypeItem *item1=self.data[0];
        NBTypeItem *item2=self.data[1];
        NBTypeItem *item3=self.data[2];
        
        [_data1 addObject:@"全部商城"];
        [_data2 addObject:@"全部热点"];
        [_data3 addObject:@"全部推荐"];
        
        for (int i=0; i<item1.screens.count; i++) {
            
            _twodata1=[NBTwoTypeItem mj_objectArrayWithKeyValuesArray:item1.screens];
            
            NBTwoTypeItem *item=_twodata1[i];
            
            [_data1 addObject:item.name];
            
            //            NSLog(@"%@",_data1);
        }
        
        for (int i=0; i<item2.screens.count; i++) {
            
            _twodata2=[NBTwoTypeItem mj_objectArrayWithKeyValuesArray:item2.screens];
            
            NBTwoTypeItem *item=_twodata2[i];
            
            [_data2 addObject:item.name];
            
            //            NSLog(@"%@",_data1);
        }
        
        for (int i=0; i<item3.screens.count; i++) {
            
            _twodata3=[NBTwoTypeItem mj_objectArrayWithKeyValuesArray:item3.screens];
            
            NBTwoTypeItem *item=_twodata3[i];
            
            [_data3 addObject:item.name];
            
            //            NSLog(@"%@",_data1);
        }
        
        
        //
    } failure:^(NSError *error) {
        
        //打印错误信息
        NSLog(@"%@",error);
        
    }];
    
    
}


- (NSInteger)numberOfColumnsInMenu:(JSDropDownMenu *)menu {
    
    return 3;
}

-(BOOL)displayByCollectionViewInColumn:(NSInteger)column{
    
    //    if (column==2) {
    //
    //        return YES;
    //    }
    //1
    return NO;
}

-(BOOL)haveRightTableViewInColumn:(NSInteger)column{
    
    //    if (column==0) {
    //        return YES;
    //    }
    //2
    return NO;
}

-(CGFloat)widthRatioOfLeftColumn:(NSInteger)column{
    
    //    if (column==0) {
    //        return 0.3;
    //    }
    //5
    return 1;
}

-(NSInteger)currentLeftSelectedRow:(NSInteger)column{
    //3
    if (column==0) {
        
        return _currentData1Index;
        
    }
    if (column==1) {
        
        return _currentData2Index;
    }
    if (column==2) {
        
        return _currentData3Index;
    }
    
    return 0;
}

- (NSInteger)menu:(JSDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column leftOrRight:(NSInteger)leftOrRight leftRow:(NSInteger)leftRow{
    //4
    if (column==0) {
        
        return _data1.count;
        
    } else if (column==1){
        
        return _data2.count;
        
    } else if (column==2){
        
        return _data3.count;
    }
    
    return 0;
}

- (NSString *)menu:(JSDropDownMenu *)menu titleForColumn:(NSInteger)column{
    
    switch (column) {
            //        case 0: return [[_data1[_currentData1Index] objectForKey:@"data"] objectAtIndex:_currentData1SelectedIndex];
        case 0: return _data1[_currentData1Index];
            
            break;
        case 1: return _data2[_currentData2Index];
            break;
        case 2: return _data3[_currentData3Index];
            break;
        default:
            return nil;
            break;
    }
}

- (NSString *)menu:(JSDropDownMenu *)menu titleForRowAtIndexPath:(JSIndexPath *)indexPath {
    
    if (indexPath.column==0) {
        
        return _data1[indexPath.row];
        
    } else if (indexPath.column==1) {
        
        return _data2[indexPath.row];
        
        
        
    } else {
        
        return _data3[indexPath.row];
        
    }
    
    
}

- (void)menu:(JSDropDownMenu *)menu didSelectRowAtIndexPath:(JSIndexPath *)indexPath {
    
    if (indexPath.column == 0) {
        
        _currentData1Index = indexPath.row;
        
        //取出选中项的ID
        if (indexPath.row==0) {
            self.mallId=@"0";
            
        }
        else
        {
        NBTwoTypeItem *item=self.twodata1[indexPath.row-1];
        //保存ID到全局属性中
        self.mallId=item.ID;
        }
        
    } else if(indexPath.column == 1){
        
        _currentData2Index = indexPath.row;
        //取出选中项的ID
        
        if (indexPath.row==0) {
            self.tagId=@"0";
            
        }
        else
        {
        NBTwoTypeItem *item=self.twodata2[indexPath.row-1];
        //保存ID到全局属性中
        self.tagId=item.ID;
        }
        
    } else{
        
        _currentData3Index = indexPath.row;
        //取出选中项的ID
        if (indexPath.row==0) {
            self.typeId=@"0";
            
        }
        else
        {
        NBTwoTypeItem *item=self.twodata3[indexPath.row-1];
        //保存ID到全局属性中
        self.typeId=item.ID;
        }
    }
    
    //清空旧数据
    self.Array=nil;
    
    self.page=1;
    
    NSString *url=[NSString stringWithFormat:@"http://mrobot.pconline.com.cn/s-300/best/faxian/getTopicList.xsp?mallId=%@&tagId=%@&typeId=%@&pageNo=1&pageSize=20&v=2.3.0&platform=android",self.mallId,self.tagId,self.typeId];
    
        [self Get:url];

    
}

@end
